import EnemyControl from "./EnemyControl";
const { ccclass, property } = cc._decorator;

@ccclass
export default class PlayControl extends cc.Component {
  @property(cc.Label)
  label: cc.Label = null;

  @property
  text: string = "hello";

  //预设体子弹
  @property(cc.Prefab)
  bulletPre: cc.Prefab = null;

  // LIFE-CYCLE CALLBACKS:

  // onLoad () {}

  start() {
    //移动监听
    let self = this;
    this.node.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
      self.node.setPosition(event.getLocation());
    });
    //自动攻击 计时器
    this.schedule(() => {
      // 创建子弹
      let bullet1 = cc.instantiate(this.bulletPre);
      let bullet2 = cc.instantiate(this.bulletPre);
      let bullet3 = cc.instantiate(this.bulletPre);
      //设置父物体
      bullet1.setParent(cc.director.getScene());
      bullet2.setParent(cc.director.getScene());
      bullet3.setParent(cc.director.getScene());
      // 设置位置
      bullet1.x = this.node.x;
      bullet1.y = this.node.y + 60;
      bullet2.x = this.node.x + 40;
      bullet2.y = this.node.y + 40;
      bullet3.x = this.node.x - 40;
      bullet3.y = this.node.y + 40;
    }, 0.1);
    // 开启碰撞检测
    cc.director.getCollisionManager().enabled = true;
  }

  update(dt) {}
  die() {
    //加载爆炸图片
    cc.loader.loadRes("hero1_die", cc.SpriteFrame, (err, res) => {
      this.node.getComponent(cc.Sprite).spriteFrame = res;
      // this.isDie = true;
    });
    //300毫秒后销毁
    setTimeout(() => {
      if (this.node) {
        this.node.destroy();
      }
    }, 300);
  }
  onCollisionEnter(other) {
    //被打中 撞上敌机
    /**@tag
     * 1玩家子弹
     * 2敌机
     * 3敌方子弹
     * 4玩家
     */
    if (other.tag == 2 || other.tag == 3) {
      this.die();
    }
  }
}
